{
Traviesadora beta 0.2.0.13.33 (30.11.2008)
LastMod: 20.03.2009 by Prof1983

Copyright 2008, travieorama@googlemail.com

This file is part of the taviesadora project

taviesadora is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

taviesadora is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
}
unit ConnectWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Jabber, IdBaseComponent, IdComponent,
  IdUDPBase, IdUDPClient, IdDNSResolver, Service, NobraProtocoll, Buttons,
  RosterGlobals, ImgList, PrefController, IdTCPConnection, IdTCPClient, IdIRC,
  TraviesadoraCore;

const
  NO_PROFILE_STRING = '(No Profile)';

type
  TFrmLogIn = class(TForm)
    pnlGround: TPanel;
    Image1: TImage;
    Image2: TImage;
    imgClose: TImage;
    Timer1: TTimer;
    ImageList1: TImageList;
    Panel4: TPanel;
    lblNobraState: TLabel;
    lblJabberstate: TLabel;
    Panel3: TPanel;
    Panel1: TPanel;
    Bevel1: TBevel;
    Panel2: TPanel;
    lblkeylibPW: TLabel;
    edtkeylibPW: TEdit;
    edtJabberPW: TEdit;
    lblJabberPW: TLabel;
    Image3: TImage;
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure chbNewUserClick(Sender: TObject);
    procedure imgCloseMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure imgCloseMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure imgCloseClick(Sender: TObject);
    procedure edtJabberPWKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure edtkeylibPWKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure SetProfile(Name:string);
  private
    btnOK: TButton;
    btnCancel: TButton;
    procedure btnOKClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
  private
    FTravCore: ITraviesadoraCoreA;
    //jabberObj: TJabber;
    NobraObj: TNobraNet;
    iniFile: TConfigFile;
    InstantConnect: Boolean;
    LoggingOut: Boolean;
    procedure LoadsbtnProfileCaption(s: string);
  protected
    procedure DoCreate; override;
  public
    constructor Init(TravCore: ITraviesadoraCoreA; NobraNet: TNobraNet; IniFile: TConfigFile; InstantConnect: Boolean; LogoutFirst: Boolean);
    procedure RefreshProfile;
  end;

var
  FrmLogIn: TFrmLogIn;

implementation

uses
  DebugWin, RosterDB;

{$R *.dfm}

{ TFrmLogIn }

procedure TFrmLogIn.Timer1Timer(Sender: TObject);
var
  Profile: TJabberProfile;
  JabberObj: TJabber;
begin
  Profile := FTravCore.ChosenProfile;
  JabberObj := FTravCore.Jabber;

  if LoggingOut then
  begin
    LoggingOut := (NobraObj.IsConnected and not InstantConnect) or JabberObj.IsConnected;
    if not(LoggingOut) then
    begin
      IniFile.SetLastusedProfile(FTravCore.ChosenProfileName);
      IniFile.SetNobraNetEnabled(Profile.NobraNetEnabled);
      IniFile.SetJabberNetEnabled(Profile.JabberEnabled);
      Profile.ssl := 1;
      if (Profile.Port = 0) then
        Profile.Port := 5222;
      jabberObj.ActivateProfile(FTravCore.ChosenProfileName);
      jabberObj.SetEnabled(Profile.JabberEnabled);
      FTravCore.StopCryptLayer(ntNobraNet);
      FTravCore.StopCryptLayer(ntJabber);
      FTravCore.CryptPW := edtkeylibPW.Text;
      if Profile.JabberEnabled then
      begin
        Timer1.Enabled := True;
        Panel1.Visible := True;
        btnOK.Enabled := False;
        if edtJabberPW.Visible then
          JabberObj.SetPassword(edtJabberPW.Text,false);
        JabberObj.Connect();
      end;
      if Profile.NobraNetEnabled then
      begin
        Timer1.Enabled := true;
        Panel1.Visible := true;
        btnOK.Enabled := false;
        iniFile.SetNobraPseudo(Profile.NobraNetPseudo);
        NobraObj.Connect;
      end else
        if NobraObj <> nil then
          NobraObj.Disconnect;
    end;
  end
  else
  begin
    if Assigned(NobraObj) then
    begin
      if Profile.NobraNetEnabled then
        lblNobraState.Caption := 'Nobra Net: '+NobraObj.GetLoginState;
      if Profile.JabberEnabled and Profile.NobraNetEnabled then
        if jabberObj.IsConnected and NobraObj.IsConnected then
        begin
          //TravCore.StartRoster(); // TODO: !!!
          Close;
        end;
      if not Profile.JabberEnabled and Profile.NobraNetEnabled then
        if NobraObj.IsConnected then
        begin
          //TravCore.StartRoster(); // TODO: !!!
          Close;
        end;
    end;
    if Profile.JabberEnabled and not Profile.NobraNetEnabled then
      if jabberObj.IsConnected then
      begin
        //TravCore.StartRoster(); // !!!
        Close;
      end;
    if Profile.JabberEnabled then
    begin
      lblJabberstate.Caption := 'Jabbber: '+jabberObj.GetLoginState;
      if JabberObj.IsLoginError then
      begin
        timer1.Enabled := False;
        Panel1.Visible := False;
        btnOK.Enabled := True;
        btnOK.Repaint;
        lblNobraState.Caption := '';
        lblJabberstate.Caption := '';
      end;
    end;
  end;
  if Profile.NobraNetEnabled and not Profile.JabberEnabled then
    if NobraObj.IsLoginError then
    begin
      timer1.Enabled := False;
      Panel1.Visible := False;
      btnOK.Enabled := True;
    end;
  if (panel2.tag = 0) then
  begin
    panel2.Width := panel2.Width+2;
    if panel2.Width+2 >= Bevel1.Width then
     panel2.Tag := 1;
  end
  else
  begin
    panel2.left := panel2.left+2;
    panel2.Width := panel2.Width-2;
    if (panel2.Width <= 0) then
    begin
      panel2.Tag := 0;
      panel2.Left := Bevel1.Left+1;
    end;
  end;
end;

procedure TFrmLogIn.btnOKClick(Sender: TObject);
begin
  if btnOK.Tag = 0 then
  begin
    LoggingOut := True;
    Timer1.Enabled := True;
    panel1.Visible := True;
    btnOK.Enabled := False;
    if LoggingOut then
    begin
      if NobraObj.IsConnected then
      begin
        lblNobraState.Caption := 'Logging out..';
        NobraObj.Disconnect;
      end;
      if FTravCore.Jabber.IsConnected then
      begin
        lblJabberstate.Caption := 'Logging out..';
        FTravCore.Jabber.Disconnect();
      end;
    end;
  end;
  {else
  begin
    sbtnEditProfile.Click;
  end;}
end;

procedure TFrmLogIn.btnCancelClick(Sender: TObject);
begin
  if not(btnOK.Enabled) then
  begin
    btnOK.Enabled := True;
    FTravCore.Jabber.CancelConnect;
    NobraObj.CancelConnect;
    Timer1.Enabled := False;
    Panel1.Visible := False;
    lblNobraState.Caption := '';
    lblJabberstate.Caption := '';
  end
  else
    Close();
end;

procedure TFrmLogIn.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

procedure TFrmLogIn.chbNewUserClick(Sender: TObject);
begin
 {
  if chbNewUser.Checked then begin
    edtPW2.Enabled := true;
    edtPW2.Color := clWindow;
  end else begin
    edtPW2.Enabled := false;
    edtPW2.Color := self.Color;
  end;      }
end;

constructor TFrmLogIn.Init(TravCore: ITraviesadoraCoreA; NobraNet: TNobraNet; IniFile: TConfigFile; InstantConnect: Boolean;LogoutFirst:boolean);
begin
  FTravCore := TravCore;
  self.InstantConnect := InstantConnect;
  btnOK.Tag := 0;
  LoggingOut := False;
  Self.IniFile := IniFile;
  Self.NobraObj := NobraNet;
  Show();
  Repaint();
  SetProfile(IniFile.GetLastusedProfile);
  if InstantConnect then
    btnOKClick(btnOK);
end;

procedure TFrmLogIn.RefreshProfile;
var
  pwPos: Integer;
  valid: Boolean;
  Profile: TJabberProfile;
begin
  valid := FTravCore.RefreshProfile();
  Profile := FTravCore.ChosenProfile;

  if (Profile = nil) or (not valid) then
  begin
    btnOK.Tag := 1;
    btnOK.Caption := 'Edit profile';
  end
  else
  begin
    btnOK.Tag := 0;
    btnOK.Caption := 'Connect';
  end;
  if Assigned(Profile) then
  begin
    LoadsbtnProfileCaption(FTravCore.ChosenProfileName);
    pwPos := - edtJabberPW.Width-10;
    if (Profile.Password = '') and (Profile.JabberEnabled) then
    begin
      edtJabberPW.Visible := True;
      lblJabberPW.Visible := True;
      pwPos := 0;
    end
    else
    begin
      edtJabberPW.Visible := False;
      lblJabberPW.Visible := False;
    end;
    if iniFile.GetKeyLibPWState then
    begin
      edtkeylibPW.Visible := True;
      lblkeylibPW.Visible := True;
      edtkeylibPW.Left := edtkeylibPW.Left+pwPos;
      lblkeylibPW.Left := lblkeylibPW.Left+pwPos;
    end
    else
    begin
      edtkeylibPW.Visible := False;
      lblkeylibPW.Visible := False;
    end;
  end;

  if not(Assigned(Profile)) then
  begin
    LoadsbtnProfileCaption(NO_PROFILE_STRING);
    edtJabberPW.Visible := False;
    lblJabberPW.Visible := False;
    edtkeylibPW.Visible := False;
    lblkeylibPW.Visible := False;
  end;
end;

procedure TFrmLogIn.SetProfile(Name: string);
begin
  FTravCore.SelectProfile(Name);
  RefreshProfile;
end;

procedure TFrmLogIn.imgCloseMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  imgClose.Top := imgClose.Top+1;
  imgClose.left := imgClose.left+1;
  imgClose.Tag := 1;
end;

procedure TFrmLogIn.imgCloseMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if imgClose.Tag = 1 then
  begin
    imgClose.Top := imgClose.Top-1;
    imgClose.left := imgClose.left-1;
    imgClose.Tag := 0;
  end;
end;

procedure TFrmLogIn.imgCloseClick(Sender: TObject);
begin
  if InstantConnect or NobraObj.IsConnected or FTravCore.Jabber.IsConnected then
    Close();
end;

procedure TFrmLogIn.LoadsbtnProfileCaption(s: string);
{var
  i: Integer;}
begin
  {
  for i := length(s) to 28 do
    s := s+' ';
  sbtnProfile.Caption := s;
  }
end;

procedure TFrmLogIn.edtJabberPWKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (key = VK_RETURN) then
  begin
    key := 0;
    btnOKClick(Sender);
  end;
end;

procedure TFrmLogIn.edtkeylibPWKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (key = VK_RETURN) then
  begin
    key := 0;
    btnOKClick(Sender);
  end;
end;

procedure TFrmLogIn.DoCreate();
begin
  inherited;

  btnOK := TButton.Create(Self);
  btnOK.Parent := Panel4;
  btnOK.Left := 144;
  btnOK.Top := 0;
  btnOK.Width := 70;
  btnOK.Height := 25;
  btnOK.Caption := 'Connect';
  btnOK.Font.Charset := DEFAULT_CHARSET;
  btnOK.Font.Color := 2434392;
  btnOK.Font.Height := -11;
  btnOK.Font.Name := 'MS Sans Serif';
  btnOK.Font.Style := [];
  btnOK.TabOrder := 0;
  btnOK.TabStop := True;
  btnOK.OnClick := btnOKClick;

  btnCancel := TButton.Create(Self);
  btnCancel.Parent := Panel4;
  btnCancel.Left := 224;
  btnCancel.Top := 0;
  btnCancel.Width := 70;
  btnCancel.Height := 25;
  btnCancel.Caption := 'Cancel';
  btnCancel.Font.Charset := DEFAULT_CHARSET;
  btnCancel.Font.Color := 2434392;
  btnCancel.Font.Height := -11;
  btnCancel.Font.Name := 'MS Sans Serif';
  btnCancel.Font.Style := [];
  btnCancel.TabOrder := 1;
  btnCancel.TabStop := True;
  btnCancel.OnClick := btnCancelClick;
end;

end.
